const models = require('../db/mysqldb/index')
const { Op } = require("sequelize");

class DataProjectModel {
  /**
   * @description 通过 project_id 查找数据，不包含删除数据
   * @param {string} project_id 
   * @returns 
   */
  static async findByProjectId (project_id,) {
    return await models.project.findOne({
      where: {
        project_id,
      }
    })
  }
  /**
   * @description 通过 name 查找数据，不包含删除数据
   * @param {string} name 
   * @returns 
   */
  static async findByProjectName (name,) {
    return await models.project.findOne({
      where: {
        name,
        delete_timestamp: {
          [Op.is]: null,
        }
      }
    })
  }
  /**
   * @description 通过 name id 查找数据，不包含删除数据
   * @param {string} name 
   * @param {string} project_id 
   * @returns 
   */
  static async findByProjectNameNeId (name, project_id) {
    return await models.project.findOne({
      where: {
        name,
        delete_timestamp: {
          [Op.is]: null,
        },
        project_id: {
          [Op.ne]: project_id,
        }
      }
    })
  }
  /**
   * @description create
   * @param {*} name 
   * @param {*} thumb 
   * @param {*} remark 
   * @param {*} op_user_id 
   * @returns 
   */
  static async create (name, thumb, remark, op_user_id) {
    return await models.project.create({
      name,
      thumb,
      remark,
      create_by: op_user_id,
      create_timestamp: Date.now()
    })
  }
  /**
   * @description 更新
   * @param {*} name 
   * @param {*} thumb 
   * @param {*} remark 
   * @param {*} op_user_id 
   * @param {*} project_id 
   * @returns 
   */
  static async update (name, thumb, remark, publish, op_user_id, project_id) {
    return await models.project.update(
      {
        name,
        thumb,
        remark,
        publish,
        update_by: op_user_id,
        update_timestamp: Date.now()
      },
      {
        where: { project_id },
      }
    )
  }
  /**
  * @description 删除
  * @param {*} op_user_id 
  * @param {*} project_id 
  * @returns 
  */
  static async delete (op_user_id, project_id) {
    return await models.project.update(
      {
        delete_timestamp: Date.now(),
        delete_by: op_user_id
      },
      {
        where: { project_id },
      }
    )
  }
  /**
   * @description 获取list
   * @param {*} limit 
   * @param {*} size 
   * @param {*} name 
   * @param {*} remark 
   * @returns 
   */
  static async list () {
    let sql = `SELECT SQL_CALC_FOUND_ROWS
    p.*,
    count( pa.NAME ) AS num 
  FROM
    project p
    LEFT JOIN page pa ON pa.project_id = p.project_id WHERE p.delete_timestamp is NULL AND pa.delete_timestamp is NULL
  GROUP BY
    p.project_id 
  ORDER BY
    create_timestamp DESC,
    update_timestamp DESC `
    const result = await models.sequelize.query(sql, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    const count = await models.sequelize.query(`SELECT FOUND_ROWS() as count;`, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    return {
      result, count
    }
  }
}
module.exports = {
  DataProjectModel,
}